{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AAS231 WWT Workshop Notebook (January 2018; Washington, DC)\n",
    "\n",
    "*Please note: these notebooks are preserved for posterity but not kept up-to-date, so you might run into issues with older notebooks.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pywwt.jupyter import WWTJupyterWidget\n",
    "from astropy.coordinates import SkyCoord, concatenate        \n",
    "from astropy import units as u        \n",
    "from astropy.table import Table, Column        \n",
    "import re\n",
    "wwt = WWTJupyterWidget()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(type(sorted(wwt._available_layers)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# init\n",
    "layers = {}\n",
    "spectrum = ['gamma', 'x', 'uv', 'visible', 'ir', 'micro', 'radio', 'other']\n",
    "\n",
    "# init\n",
    "for band in spectrum:\n",
    "    layers[band] = {}\n",
    "\n",
    "# method\n",
    "def shorten(string):\n",
    "    first = string[:re.search(r'[\\W]', string).start()].lower()\n",
    "    \n",
    "    # just for 2MASS; use num2words to standardize process?\n",
    "    check = re.search(r'2', first)\n",
    "    if(check != None):\n",
    "        first = first[:check.start()] + 'two' + first[check.start()+1:]\n",
    "        \n",
    "    return first\n",
    "\n",
    "# method\n",
    "def add2dict(diction, full_layer, ind):\n",
    "    abbr = shorten(full_layer)\n",
    "    \n",
    "    # check if abbr exists\n",
    "    j = 2\n",
    "    while(abbr in diction[spectrum[ind]]):\n",
    "        if(str(j-1) in abbr):\n",
    "            abbr = abbr[:-1]\n",
    "            \n",
    "        abbr += str(j)\n",
    "        j += 1\n",
    "    \n",
    "    diction[spectrum[ind]][abbr] = {}\n",
    "    diction[spectrum[ind]][abbr]['full_name'] = full_layer\n",
    "    diction[spectrum[ind]][abbr]['thumbnail'] = None\n",
    "    \n",
    "# init\n",
    "for layer in wwt.available_layers:\n",
    "    ind = 0\n",
    "    \n",
    "    if(re.search(r'(?i)gamma',layer) != None):\n",
    "        add2dict(layers, layer, ind)\n",
    "        continue #automatically advance to next iteration\n",
    "    \n",
    "    ind += 1\n",
    "    if(re.search(r'(?i)x(-|\\s)?ray',layer) != None):\n",
    "        add2dict(layers, layer, ind)\n",
    "        continue\n",
    "        \n",
    "    ind += 1\n",
    "    if(re.search(r'(?i)ultra(-|\\s)?violet',layer) != None or \n",
    "         re.search(r'(?i)[^\\d\\w]+uv|uv[^\\d\\w]+',layer) != None):\n",
    "        add2dict(layers, layer, ind)\n",
    "        continue\n",
    "        \n",
    "    ind += 1\n",
    "    if(re.search(r'(?i)optical',layer) != None or \n",
    "         re.search(r'(?i)visible',layer) != None):\n",
    "        add2dict(layers, layer, ind)\n",
    "        continue\n",
    "    \n",
    "    ind += 1    \n",
    "    if(re.search(r'(?i)infrared',layer) != None or \n",
    "         re.search(r'(?i)[^\\d\\w]+ir|ir[^\\d\\w]+',layer) != None):\n",
    "        add2dict(layers, layer, ind)\n",
    "        continue\n",
    "        \n",
    "    ind += 1\n",
    "    if(re.search(r'(?i)microwave',layer) != None or \n",
    "         re.search(r'(?i)[^\\d\\w]+cmb|cmb[^\\d\\w]+',layer) != None):\n",
    "        add2dict(layers, layer, ind)\n",
    "        continue\n",
    "\n",
    "    ind += 1\n",
    "    if(re.search(r'(?i)radio',layer) != None):\n",
    "        add2dict(layers, layer, ind)\n",
    "        continue\n",
    "\n",
    "    ind += 1\n",
    "    add2dict(layers, layer, ind)\n",
    "\n",
    "layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#available_layers = {}\n",
    "available_layers['visible'] = {'twomass': {'full_name': '2mass survey', 'thumbnail': None},'iras': {'full_name': 'iras', 'thumbnail': None}}\n",
    "    \n",
    "class ImageryLayers:\n",
    "   def __init__(self, layers_dict):\n",
    "       self._layers = layers_dict\n",
    "\n",
    "   def __dir__(self):\n",
    "       return sorted(self._layers.keys())\n",
    "\n",
    "   def __getattr__(self, name):\n",
    "       return Bandpass(self._layers[name])\n",
    "\n",
    "class Bandpass:\n",
    "   def __init__(self, layers):\n",
    "       self._layers = layers\n",
    "\n",
    "   def __dir__(self):\n",
    "       return sorted(self._layers.keys())\n",
    "\n",
    "   def __getattr__(self, name):\n",
    "       return self._layers[name]['full_name']\n",
    "\n",
    "imagery = ImageryLayers(available_layers)\n",
    "print(imagery.visible.twomass)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dice = {}\n",
    "dice['raw'] = 'cool'\n",
    "dice['2time'] = 'amen'\n",
    "dice['lie'] = []\n",
    "k = 'lie'\n",
    "dice[k].append('kant')\n",
    "dice['mine'] = {'from':'Texas', 'to': 'Forever'}\n",
    "dice['mine'].update({'first':'Biff', 'last': 'Anthony'})\n",
    "dice['what'] = ['do']#['you'] = 'want'\n",
    "print(dice)\n",
    "type(wwt.available_layers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(re.search(r'.*ious\\b','laborious'), re.match(r'.*ious\\b','laborious'), sep='\\n')\n",
    "print(re.search(r'(?i)optical',wwt.background) != None)\n",
    "print(re.search(r'(?i)[^\\d\\w]+uv|uv[^\\d\\w]+',wwt.background) != None)\n",
    "fly = '__536_'\n",
    "print(re.search(r'\\d',fly).group(0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "integers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']\n",
    "string = ' __ !*22more wins'\n",
    "\n",
    "cut_left = re.search(r'^[_\\W]+', string)\n",
    "if cut_left is not None:\n",
    "    string = string[cut_left.end():]\n",
    "\n",
    "cut_right = re.search(r'\\W', string)\n",
    "if cut_right is not None:\n",
    "    string = string[:cut_right.start()].lower()\n",
    "\n",
    "digit = re.search(r'^\\d', string)\n",
    "if digit is not None:\n",
    "    for i, num in enumerate(integers, 0):\n",
    "        if str(i) == digit.group(0):\n",
    "            string = string[:digit.start()] + num + string[digit.end():]\n",
    "            break\n",
    "        else:\n",
    "            continue\n",
    "            \n",
    "print(string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#wwt.add_circle(radius=10*u.arcsec,line_width=3*u.pix,line_color='#008ca8',label='heathen',hover_label=True)\n",
    "wwt.clear_annotations()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.center_on_coordinates(SkyCoord.from_name('Megrez'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.crosshairs = False\n",
    "wwt.layer_controls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "# open widget, render at end of each section\n",
    "wwt = WWTQtWidget(size=(600,400))\n",
    "'''\n",
    "# big_dipper.png\n",
    "bd = concatenate((SkyCoord.from_name('Alkaid'), SkyCoord.from_name('Mizar'),\n",
    "                  SkyCoord.from_name('Alioth'), SkyCoord.from_name('Megrez'),\n",
    "                  SkyCoord.from_name('Phecda'), SkyCoord.from_name('Merak'),\n",
    "                  SkyCoord.from_name('Dubhe')))\n",
    "wwt.center_on_coordinates(SkyCoord.from_name('Megrez'))\n",
    "       \n",
    "line = wwt.add_line(bd, width=3 * u.pixel, tag='whoa now', hover_label=True)\n",
    "#wwt.render('big_dipper.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# big_dipper2.png\n",
    "line.add_point(SkyCoord.from_name('Megrez'))\n",
    "line.color = 'salmon'; line.opacity=.3\n",
    "#wwt.render('big_dipper2.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# polygon.png (1)\n",
    "wwt.center_on_coordinates(SkyCoord.from_name('eta orion'))\n",
    "\n",
    "body = concatenate((SkyCoord.from_name('zeta orion'),\n",
    "                    SkyCoord.from_name('betelgeuse'),\n",
    "                    SkyCoord.from_name('bellatrix'),\n",
    "                    SkyCoord.from_name('delta orion')))\n",
    "club = concatenate((SkyCoord.from_name('xi orionis'),\n",
    "                    SkyCoord.from_name('chi2 orionis'),\n",
    "                    SkyCoord.from_name('chi1 orionis'),\n",
    "                    SkyCoord.from_name('67 orionis')))\n",
    "head = concatenate((SkyCoord.from_name('betelgeuse'),\n",
    "                    SkyCoord.from_name('lambda orion'),\n",
    "                    SkyCoord.from_name('bellatrix')))\n",
    "bow = concatenate((SkyCoord.from_name('omicron2 orionis'),\n",
    "                   SkyCoord.from_name('pi2 orionis'),\n",
    "                   SkyCoord.from_name('pi3 orionis'),\n",
    "                   SkyCoord.from_name('pi4 orionis'),\n",
    "                   SkyCoord.from_name('pi5 orionis')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# polygon.png (2)\n",
    "a1 = concatenate((SkyCoord.from_name('betelgeuse'),\n",
    "                  SkyCoord.from_name('xi orionis')))\n",
    "a2 = concatenate((SkyCoord.from_name('bellatrix'),\n",
    "                  SkyCoord.from_name('pi3 orionis')))\n",
    "l1 = concatenate((SkyCoord.from_name('zeta orionis'),\n",
    "                  SkyCoord.from_name('kappa orionis')))\n",
    "l2 = concatenate((SkyCoord.from_name('delta orionis'),\n",
    "                  SkyCoord.from_name('eta orionis'),\n",
    "                  SkyCoord.from_name('rigel')))\n",
    "blt = concatenate((SkyCoord.from_name('zeta orionis'),\n",
    "                    SkyCoord.from_name('delta orionis')))\n",
    "string = concatenate((SkyCoord.from_name('omicron2 orionis'),\n",
    "                      SkyCoord.from_name('pi5 orionis')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# polygon.png (3)\n",
    "orb = wwt.add_polygon(body, fill=True, fill_color='lightslategray', opacity=.4,\n",
    "                      line_color='lightslategray', line_width=3*u.pixel)\n",
    "orc = wwt.add_polygon(club, fill=True, fill_color='saddlebrown', opacity=.4,\n",
    "                      line_color ='saddlebrown', line_width=3 * u.pixel)\n",
    "orh = wwt.add_polygon(head, fill=True, fill_color='rosybrown', opacity=.4,\n",
    "                      line_color='lightslategray', line_width=2*u.pixel)\n",
    "orw = wwt.add_polygon(bow, opacity=.4, \n",
    "                      line_width=2*u.pixel, line_color='saddlebrown')\n",
    "\n",
    "ora1 = wwt.add_line(a1, color='lightslategray')\n",
    "ora2 = wwt.add_line(a2, color='lightslategray')\n",
    "orl1 = wwt.add_line(l1, color='lightslategray', width=2*u.pixel)\n",
    "orl2 = wwt.add_line(l2, color='lightslategray', width=2*u.pixel)\n",
    "orblt = wwt.add_line(blt, color='azure', width=8*u.pixel)\n",
    "orstr = wwt.add_line(string, color='azure')\n",
    "\n",
    "#wwt.render('polygon.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# circles.png\n",
    "wwt.center_on_coordinates(SkyCoord(190, -55, unit=u.deg))\n",
    "crc1 = wwt.add_circle(SkyCoord(188, -57, unit=u.deg), radius=10 * u.degree,\n",
    "                      fill=True, fill_color='#008CA8')\n",
    "crc2 = wwt.add_circle(radius=10 * u.pixel, opacity=.4,\n",
    "                      fill=True, fill_color='#C4D600')\n",
    "#wwt.render('circles.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# stgo_view.png\n",
    "wwt.constellation_boundaries = True\n",
    "wwt.constellation_figures = True\n",
    "wwt.constellation_boundary_color = 'azure'\n",
    "wwt.constellation_figure_color = '#D3BC8D'\n",
    "wwt.constellation_selection_color = (1, 0, 1)\n",
    "wwt.local_horizon_mode = True\n",
    "wwt.location_latitude = -33.4172 * u.deg\n",
    "wwt.location_longitude = -70.604 * u.deg\n",
    "wwt.location_altitude = 300 * u.meter\n",
    "#wwt.render('stgo_view.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# dust_on_gamma.png\n",
    "wwt.clear_annotations()\n",
    "wwt.constellation_boundaries = False\n",
    "wwt.constellation_figures = False\n",
    "wwt.local_horizon_mode = False\n",
    "wwt.crosshairs = False\n",
    "\n",
    "wwt.center_on_coordinates(SkyCoord(144.545, -68.5, unit=u.deg))\n",
    "wwt.background = 'Fermi LAT 8-year (gamma)'\n",
    "wwt.foreground = 'Planck Dust & Gas'\n",
    "wwt.foreground_opacity = .75\n",
    "#wwt.render('dust_on_gamma.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#wwt.center_on_coordinates(SkyCoord.from_name('megrez'), fov = 10*u.deg)\n",
    "wwt.center_on_coordinates(SkyCoord.from_name('eta orion'), fov = 30*u.deg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.clear_annotations()\n",
    "#wwt.crosshairs = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Credits\n",
    "\n",
    "This notebook was prepared by O. Justin Otor."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
